#include"header/MapPredator.hpp"
//#include "header/TypeResource.h"
#include<map>
#include <utility>
#include <iostream>


using namespace std;

/**
 * Add the prey for the predator given in argument.
 * (Also add the predator for the prey given in argument)
 */
void MapPredator::addPredator(string const& pred, string const& prey) {
    /******** Fill the map Predator->Prey ********/

    // Check if the key exists
    map_animal::iterator iter = predators.find(pred);

    // We check that the predator exists
    if (iter != predators.end()) {
        // Add the new prey if not exists
        anim::iterator iter_pred = find((*iter).second.begin(), (*iter).second.end(), prey);

        // Check the prey exists
        if (iter_pred == (*iter).second.end()) {
            (*iter).second.push_back(prey);
        }
    } else {
        // Create a new pair Predator->Prey
        anim a;
        a.push_back(prey);
        predators.insert(make_pair(pred, a));
    }

    // Add the prey to the predator map
    addPrey(prey, pred);
}

/**
 * Check if the predator is really a predator of the prey given in argument.
 */
bool MapPredator::isPredator(string const& pred, string const& prey) const {
    map_animal::const_iterator iter = predators.find(pred);

    // We check that the predator exists
    if (iter != predators.end()) {
        anim::const_iterator iter_pred = find((*iter).second.begin(), (*iter).second.end(), prey);

        // Check if it is a prey
        if (iter_pred != (*iter).second.end()) {
            return true;
        }
    }
    return false;
}

/**
 * Check if the prey is really a prey of the predator given in argument.
 */
bool MapPredator::isPrey(string const& pred, string const& prey) const {
    map_animal::const_iterator iter = preys.find(prey);

    // We check that the prey exists
    if (iter != preys.end()) {
        anim::const_iterator iter_prey = find((*iter).second.begin(), (*iter).second.end(), pred);

        // Check if it is a pred
        if (iter_prey != (*iter).second.end()) {
            return true;
        }
    }
    return false;
}

/******** Fill the map Prey->Predator ********/
void MapPredator::addPrey(string const& prey, string const& pred) {
    // Check if the key exists
    map_animal::iterator iter = preys.find(prey);

    // We check that the prey exists
    if (iter != preys.end()) {
        // Add the new predator if not exists
        anim::iterator iter_prey = find((*iter).second.begin(), (*iter).second.end(), pred);

        // Check the predator exists
        if (iter_prey == (*iter).second.end()) {
            (*iter).second.push_back(pred);
        }
    } else {
        // Create a new pair Prey->Predator
        anim a;
        a.push_back(pred);
        preys.insert(make_pair(prey, a));
    }
}
